package com.cheer.service.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.cheer.entity.OaAdminEntity;
import com.cheer.entity.OaLearnClassEntity;
import com.cheer.enums.CheckStatusEnum;
import com.cheer.exception.ServiceException;
import com.cheer.security.UserInfoHolder;
import com.cheer.service.*;
import com.cheer.vo.IndexStatisticsVO;
import com.cheer.vo.LecIndexOrgVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class LecIndexServiceImpl implements LecIndexService {

    @Autowired
    private LecTeacherService lecTeacherService;


    @Autowired
    private LecCourseService lecCourseService;

    @Autowired
    private UserInfoHolder userInfoHolder;

    @Autowired
    private OaLearnClassService oaLearnClassService;


    @Override
    public IndexStatisticsVO statistics() {

        // 查询当前管理人
        Integer orgId = userInfoHolder.getAdmin().getPid();
        String path = "";
        if(orgId != OaLearnClassServiceImpl.TOP_LEVEL){
            OaLearnClassEntity oaLearnClassEntity = oaLearnClassService.getById(orgId);
            path = oaLearnClassEntity.getPath() + oaLearnClassEntity.getId() + ",";
        }
        IndexStatisticsVO indexStatisticsVO = new IndexStatisticsVO();
        int lecTeacherCount = lecTeacherService.countByPath(path);
        indexStatisticsVO.setTeacherCount(lecTeacherCount);
        int lecCourseCount = lecCourseService.countByPath(path);
        indexStatisticsVO.setCourseCount(lecCourseCount);
        // 未批乐讲座
        int courseToReadCount = lecCourseService.countByStatus(CheckStatusEnum.IS_SUB, path);
        indexStatisticsVO.setToReadCourseCount(courseToReadCount);
        // 已通过讲座数
        int courseFinishCount = lecCourseService.countByStatus(CheckStatusEnum.IS_PASS, path);
        indexStatisticsVO.setFinishCourseCount(courseFinishCount);

        return indexStatisticsVO;
    }

    @Override
    public LecIndexOrgVO statisticsOrg(String year, String org) {

        if(StrUtil.isBlank(year)){
            throw new ServiceException("请选择年份！");
        }
        if(ObjectUtil.isNull(org)){
            throw new ServiceException("请选择机构！");
        }
        if(NumberUtil.isNumber(org) == false){
            throw new ServiceException("请选择机构！");
        }
        // 获取当前机构下的路径
        LecIndexOrgVO lecIndexOrgVO = lecCourseService.getStatisticsOrg(year, org);

        return lecIndexOrgVO;
    }
}
